<?php
$data->uis = DbUtils::get($myPdo, 'pz2_ui', NULL, NULL, NULL, NULL, NULL, NULL)->d;

$cond_vals = new stdClass();
$cond_vals->c = 't.sys_id = :v1';
$cond_vals->v = array(':v1' => $system_id);
$opts = new stdClass();
$opts->select_expr = 't.id, t.parent_id, t.title, t.order';
$data->pages = DbUtils::get($myPdo, 'pz2_page', $cond_vals, 't.order', 'ASC', NULL, NULL, $opts)->d;
?>
<style>
.form {
  background-color: #fff;
}
.form td {
  vertical-align: middle;
  padding: 2px 5px;
}
.form th.header {
  text-align: left;
  color: #fff;
  background-color: #e09696;
  padding-left: 5px;
  font-weight: bold;
  height: 25px;
}
.form td.title {
  background-color: #f1d0d0;
  border-top: 1px solid #fff;
  width: 150px;
}
.form td.content {
  padding: 0;
}
.form td.content input {
  width: 99%;
  border: solid 1px #ddd;
}
.form td.content select {
  width: 100%;
  border: solid 1px #ddd;
}
.form td.content textarea {
  resize: none;
  width: 99%;
  height: 70px;
  border: solid 1px #ddd;
  margin: 2px 0;
}
</style>
<table class="form" cellPadding=0 cellSpacing=0>
  <tr><th class="header" colspan=2>Basic</th></tr>
  <tr>
    <td class="title">Title</td>
    <td class="content"><input type="text" id="title" value="<?php echo isset($data->sys) ? $data->sys->title : ''; ?>"></input></td>
  </tr>
  <tr>
    <td class="title">URL</td>
    <td class="content">http://<?php echo URL; ?><span><input type="text" id="script" style="width: 100px;" value="<?php echo isset($data->sys) ? $data->sys->script : ''; ?>"></input></span>.php</td>
  </tr>
  <tr>
    <td class="title">Theme</td>
    <td class="content">
      <select id="ui">
        <option value="0">-</option>
        <?php foreach ($data->uis as &$ui) { ?>
          <option value="<?php echo $ui->id; ?>" <?php if (isset($data->sys) && $data->sys->ui_id == $ui->id) echo 'selected="selected"'; ?>><?php echo $ui->folder; ?></option>
        <?php } ?>
      </select>
    </td>
  </tr>
  <tr><th class="header" colspan=2>Authentication</th></tr>
  <tr>
    <td class="title">Required</td>
    <td><input  id="is_login_required" type="checkbox" <?php if (isset($data->sys) && $data->sys->is_login_required == 1) echo 'checked="true"'; ?>></input></td>
  </tr>
  <tr>
    <td class="title">Login Page</td>
    <td class="content"><select id="login_page"><option value="0">-</option></select></td>
  </tr>
  <tr><th class="header" colspan=2>SEO</th></tr>
  <tr>
    <td class="title">Description</td>
    <td class="content"><textarea  id="description"><?php echo isset($data->sys) ? $data->sys->keywords : ''; ?></textarea></td>
  </tr>
  <tr>
    <td class="title">Key Words</td>
    <td class="content"><textarea  id="keywords"><?php echo isset($data->sys) ? $data->sys->description : ''; ?></textarea></td>
  </tr>
</table>
<span style="margin-top: 5px;" class="image_button" onclick="check_save();">Save</span>
<?php if (isset($data->sys)) { ?>
<span style="margin-top: 5px;" class="image_button" onclick="my_remove();">Delete</span>
<?php } ?>

<script type="text/javascript" src="libs/jquery/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="incls/js/pageutils.js"></script>
<script type="text/javascript" src="incls/js/myutils.js"></script>
<script type="text/javascript" src="incls/js/checkutils.js"></script>
<script type="text/javascript">
  var _check_add = [];
  var _errors_add = [];
  
  $(function(){
    set_parent(PageUtils.getRoot(JSON.parse('<?php echo addslashes(json_encode($data->pages)); ?>')), $('#login_page')[0], '');
  });
  
  function set_parent(node, elm, prefix) {
    if (node.id != 0) {
      var opt = new Option(((prefix == '') ? '' : prefix + ') ' ) + ((node.id != 0 && node.title == '') ? 'Untitled' : node.title), node.id);
      elm.options[elm.options.length] = opt;
      <?php if (isset($data->sys)) { ?>
      if ('<?php echo $data->sys->login_page_id; ?>' == node.id) {
        elm.selectedIndex = elm.options.length - 1;
      }
      <?php } ?>
    }
    for (var idx in node._children) {
      var obj = node._children[idx];
      set_parent(obj, elm, ((prefix == '') ? '(' : '\u2002' + prefix + '-' ) + (parseInt(idx, 10) + 1));
    }
  }
  
  function check_save() {
    CheckUtils.addToArray(_check_add, $('#title')[0], check_empty, _errors_add);
    CheckUtils.addToArray(_check_add, $('#script')[0], check_script, _errors_add);
    save();
  }

  function save() {
    if (_check_add.length > 0) {
      setTimeout('save();', 500);
      return;
    }
    if (_errors_add.length > 0) {
      return;
    }

    if ($('#ui')[0].options[$('#ui')[0].selectedIndex].value == 0) {
      return;
    }

    var obj = {
      <?php if (isset($data->sys)) { ?>
      id: <?php echo $data->sys->id; ?>,
      _script: '<?php echo $data->sys->script; ?>',
      <?php } ?>
      script: $('#script')[0].value,
      ui_id: $('#ui')[0].options[$('#ui')[0].selectedIndex].value,
      is_login_required: $('#is_login_required')[0].checked ? 1 : 0,
      login_page_id: $('#login_page')[0].options[$('#login_page')[0].selectedIndex].value,
      title: $('#title')[0].value,
      description: $('#description')[0].value,
      keywords: $('#keywords')[0].value
    };
    $.ajax({
      type: 'POST',
      url: '<?php echo $path . '/' . basename(__FILE__, '.php'); ?>.server.php',
      data: 'action=save&q=' + encodeURIComponent(JSON.stringify(obj)),
      success: function(msg){
        try {
          var result = JSON.parse(msg);
          location.href = 'http://<?php echo URL . 'instls.php?mod=system&menu=systems&sid='; ?>' + result.id;
        } catch (ex) {
          alert( 'Error: ' + ex );
        }
      }
    });
  }

  function my_remove() {
    var r = window.confirm('Click OK to Proceed?');
    if (r) {
      $.ajax({
        type: 'POST',
        url: '<?php echo $path . '/' . basename(__FILE__, '.php'); ?>.server.php',
        data: 'action=remove&q=' + encodeURIComponent('<?php echo isset($data->sys) ? (json_encode($data->sys)) : '[]'; ?>'),
        success: function(msg){
          try {
            JSON.parse(msg);
            location.href = 'http://<?php echo URL . 'instls.php?mod=systems&menu=systems'; ?>';
          } catch (ex) {
            alert( 'Error: ' + ex );
          }
        }
      });
    }
  }

  function check_script(inputElm, callbackFunc, errorArr) {
    if (inputElm.value == ''){
      CheckUtils.addError(inputElm, errorArr, 'Required');
    } else {
      $.ajax({
        type: 'POST',
        url: '<?php echo $path . '/' . basename(__FILE__, '.php'); ?>.server.php',
        data: 'action=check_script&value=' + encodeURIComponent(inputElm.value) + '&sid=<?php echo $system_id; ?>',
        success: function(msg){
          try {
            var obj = JSON.parse(msg);
            if (obj.length > 0) {
              CheckUtils.addError(inputElm, errorArr, 'Exist');
            }
            callbackFunc();
          } catch (ex) {
            alert( 'Error: ' + ex );
          }
        }
      });
      return;
    }
    callbackFunc();
  }

  function check_empty(inputElm, callbackFunc, errorArr) {
    if (inputElm.value == ''){
      CheckUtils.addError(inputElm, errorArr, 'Required');
    }
    callbackFunc();
  }
</script>